package com.yxhl.zoume.common.ui.fragment;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.adapter.UpPullLoadDataAdapter;
import com.yxhl.zoume.common.presenter.UpPullPresenter;
import com.yxhl.zoume.common.ui.view.UpPullRefreshView;
import com.yxhl.zoume.core.tripsmgmt.presenter.home.HistoryTripPresenter;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.PromptUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * created by sugarya
 */
public abstract class UpPullBaseFragment<T> extends BaseFragment implements UpPullRefreshView<T> {

    private static final String TAG = "UpPullBaseFrag";

    //widgets
    @BindView(R.id.srl_include_refresh_layout)
    protected SwipeRefreshLayout mSwipeRefreshLayout;

    @BindView(R.id.recycler_include_recycler_view)
    protected RecyclerView mRecyclerView;

    @BindView(R.id.iv_include_refresh_no_record)
    protected ImageView mNoRecordIv;


    //arguments
    protected List<T> mDataList;

    protected UpPullLoadDataAdapter mUpPullLoadDataAdapter;
    protected UpPullLoadDataAdapter.OnUpPullLoadDataScrollListener mOnUpPullLoadDataScrollListener;


    public UpPullBaseFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        initializeSwipeRefreshLayout(mSwipeRefreshLayout);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initializeRecyclerView(mRecyclerView);
    }

    protected void initializeSwipeRefreshLayout(SwipeRefreshLayout swipeRefreshLayout) {
        swipeRefreshLayout.setColorSchemeResources(
                R.color.bg_color_orange,
                R.color.bg_color_green,
                R.color.colorAccent);
        swipeRefreshLayout.setOnRefreshListener(this.new OnPassengerRefreshListener());
    }

    protected void initializeRecyclerView(RecyclerView recyclerView) {
        if (mDataList == null) {
            mDataList = new ArrayList<>();
        } else {
            mDataList.clear();
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        mUpPullLoadDataAdapter = getUpPullLoadDataAdapter(layoutManager);
        recyclerView.setAdapter(mUpPullLoadDataAdapter);

        if (mOnUpPullLoadDataScrollListener == null) {
            mOnUpPullLoadDataScrollListener = mUpPullLoadDataAdapter.new OnUpPullLoadDataScrollListener();
        }
        recyclerView.addOnScrollListener(mOnUpPullLoadDataScrollListener);
    }

    @Override
    public void renderDataForUpPullOnSuccess(List<T> dataList) {
        List<T> filteredData = getFilteredData(dataList, mDataList);
        if (filteredData != null && filteredData.size() > 0) {
            mNoRecordIv.setVisibility(View.INVISIBLE);
            mDataList.addAll(filteredData);
            mUpPullLoadDataAdapter.notifyDataSetChanged();
        } else {
            mNoRecordIv.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void noMoreUpPullData() {
        PromptUtils.showShort(mActivity, getString(R.string.history_trips_up_pull_refresh_no_more_data));
    }

    @Override
    public void closeUpPullRefresh() {
        mUpPullLoadDataAdapter.setFootViewVisibility(false);
    }

    /**
     * 去除上拉加载返回的所有数据里的重复数据
     *
     * @param dataList       服务器返回的数据集合
     * @param originDataList 列表上显示的数据集合
     * @return
     */
    protected List<T> getFilteredData(List<T> dataList, List<T> originDataList) {
        if (dataList != null && dataList.size() > 0) {
            if (originDataList != null && originDataList.size() > 0) {
                int originSize = originDataList.size();
                //需要遍历的次数()
                int amount;
                if (originSize < HistoryTripPresenter.PAGE_SIZE) {
                    amount = originSize;
                } else {
                    amount = HistoryTripPresenter.PAGE_SIZE;
                }
                amount = originSize - amount;
                for (int i = originSize - 1; i >= amount; i--) {
                    if (dataList.contains(originDataList.get(i))) {
                        dataList.remove(originDataList.get(i));
                    }
                }
            }
            return dataList;
        }
        return null;
    }

    @Override
    public void onDestroyView() {
        if (mOnUpPullLoadDataScrollListener != null) {
            if (mRecyclerView != null) {
                mRecyclerView.removeOnScrollListener(mOnUpPullLoadDataScrollListener);
            }
        }
        super.onDestroyView();
    }


    /**
     * 获取列表的适配器Adapter
     *
     * @param layoutManager
     * @return
     */
    protected abstract
    @NonNull
    UpPullLoadDataAdapter getUpPullLoadDataAdapter(LinearLayoutManager layoutManager);

    /**
     * 获取当前对应的Presenter
     *
     * @return
     */
    protected abstract UpPullPresenter getUpPullPresenter();


    public class OnPassengerRefreshListener implements SwipeRefreshLayout.OnRefreshListener {
        @Override
        public void onRefresh() {
            LOG.e(TAG, "开启下拉刷新网络请求");
            UpPullPresenter upPullPresenter = getUpPullPresenter();
            if (upPullPresenter != null) {
                upPullPresenter.loadDownRefreshData();
            }
        }
    }

}
